'use strict';

/* 刷新 */

var React = require('react-native');

var {
	AndroidPullToRefreshView
} = require('react-native-yankai-refresh');

var TimerMixin = require('react-timer-mixin');


let {
	StyleSheet,
	View,
	ScrollView,
	Text,
	Image,
	ToastAndroid,
} = React;

var {
	Log
} = require('react-native-yankai-utils');

module.exports = React.createClass({

	mixins: [
		TimerMixin
	],

	getInitialState: function() {
		return {

		}
	},

	render: function() {

		var items = [];
		for (var i = 0; i < 100; i++) {
			items.push(
				<View style={styles.contentItem}>
					<Text>Content Item</Text>
				</View>
			)
		};

		let scrollViewStyles = [
			styles.content
		];

		return (
			<AndroidPullToRefreshView
				ref="REFRESH_VIEW"
				style={styles.container}
				timeout={5000}
				onRefresh={this.refreshData}
				onTimeout={this.refreshTimeout}
				colors="red,blue"
				size="large">
			 	<ScrollView style={scrollViewStyles}>
					{items}
				</ScrollView>
			</AndroidPullToRefreshView>
		)
	},

	refreshData: function() {
		this.setTimeout(this.refreshDataCallback, 10000);
	},

	refreshTimeout: function() {
		ToastAndroid.show('数据更新失败', ToastAndroid.SHORT);
	},

	refreshDataCallback: function() {
		var refreshView = this.refs['REFRESH_VIEW'];
		refreshView.endRefresh();
		ToastAndroid.show('数据更新成功', ToastAndroid.SHORT);
	},

});


var styles = StyleSheet.create({

	container: {
		flex: 1,
		backgroundColor: 'blue',
	},

	content: {
		height: 500
	},

	contentItem: {
		backgroundColor: 'green',
		padding: 15,
		marginBottom: 1,
	}

});